package com.yunlong.softpark.controller;

import com.yunlong.softpark.common.enums.BaseErrorCodeEnum;
import com.yunlong.softpark.core.exception.SysException;
import com.yunlong.softpark.core.support.web.controller.BaseController;
import com.yunlong.softpark.core.wrapper.ResultWrapper;
import com.yunlong.softpark.dto.*;
import com.yunlong.softpark.form.ColumnSimpForm;
import com.yunlong.softpark.form.CreateColumnForm;
import com.yunlong.softpark.form.UpdateColumnForm;
import com.yunlong.softpark.service.ColumnService;
import com.yunlong.softpark.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Cui
 * @Date: 2020/7/27
 * @Description:
 */
@Api(value = "ColumnController",tags = {"栏目接口"})
@RestController
@Slf4j
@RequestMapping("/column")
public class ColumnController extends BaseController<UserInfo> {


    @Autowired
    ColumnService columnService;

    @Autowired
    RecordService recordService;


    /**
     * 创建column
     *
     * @param createColumnForm
     */
    @ApiOperation("创建栏目")
    @PostMapping("/createColumn")
    public ResultWrapper createColumn(@Valid @RequestBody CreateColumnForm createColumnForm,
                                      BindingResult bindingResult,
                                      HttpServletRequest request) {
        try {
            validateParams(bindingResult);
            String columnId = columnService.createColumn(createColumnForm, getCurrentUserInfo().getUserId());
            recordService.saveRecord(request.getRemoteAddr(),6,columnId,getCurrentUserInfo().getUserId());
            MessageSuccessDto messageSuccessDto = new MessageSuccessDto();
            messageSuccessDto.setMessage("创建成功，正在等待管理员审核！");
            return ResultWrapper.successWithData(messageSuccessDto);
        }catch (DataIntegrityViolationException e){
            log.info("record表违反唯一约束，但是插入成功");
            MessageSuccessDto messageSuccessDto = new MessageSuccessDto();
            messageSuccessDto.setMessage("创建成功，正在等待管理员审核！");
            return ResultWrapper.successWithData(messageSuccessDto);
        }  catch (Exception e) {
            log.info("ColumnController.createColumn");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 更新column
     *
     * @param updateColumnForm
     */
    @ApiOperation("更新栏目")
    @PostMapping("/updateColumn")
    public ResultWrapper updateColumn(@Valid @RequestBody UpdateColumnForm updateColumnForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            MessageSuccessDto column = columnService.updateColumn(updateColumnForm, getCurrentUserInfo().getUserId());
            return ResultWrapper.successWithData(column);
        } catch (Exception e) {
            log.info("ColumnController.updateColumn");
            return ResultWrapper.failure(e.getMessage());
        }

    }

    /**
     * 根据栏目的下载量返回前十的栏目
     *
     * @return
     */
    @ApiOperation("排行榜")
    @RequestMapping(path = "/rank", method = RequestMethod.GET)
    public ResultWrapper getRankData() {

        List<RankDto> list = columnService.getRankData();
        return ResultWrapper.successWithData(list);

    }

    /**
     * 根据columnId 搜索出栏目的简单介绍
     *
     * @param columnSimpForm
     * @return
     */
    @ApiOperation("获取栏目的简单介绍")
    @RequestMapping(path = "/simpleIntro", method = RequestMethod.POST)
    public ResultWrapper getSimpleIntroduce(@Valid @RequestBody ColumnSimpForm columnSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            ColumnSimDto columnSimDto = columnService.getSimpleIntroduce(columnSimpForm.getColumnId());
            return ResultWrapper.successWithData(columnSimDto);
        } catch (Exception e) {
            log.info("ColumnController.getSimpleIntroduce");
            return ResultWrapper.failure(e.getMessage());
        }

    }

    /**
     * 根据columnId 搜出出栏目的详细介绍
     */
    @ApiOperation("获取栏目的简单介绍")
    @RequestMapping(path = "/detailIntro", method = RequestMethod.POST)
    public ResultWrapper getDetailIntroduce(@Valid @RequestBody ColumnSimpForm columnSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            ColumnDetailDto columnDetailDto = columnService.getDetailIntroduce(columnSimpForm.getColumnId());
            List<ColumnDetailListDto> list = columnService.changeColumnDetailDtoToList(columnDetailDto);
            return ResultWrapper.successWithData(list);
        } catch (Exception e) {
            log.info("ColumnController.getDetailIntroduce");
            return ResultWrapper.failure(e.getMessage());
        }


    }

    /**
     * 《个人中心界面》
     * 根据columnID 回显栏目的相关信息提供修改
     */
    @ApiOperation("为修改栏目提供栏目回显信息")
    @RequestMapping(path = "/personal/detailData", method = RequestMethod.POST)
    public ResultWrapper getDetailData(@Valid @RequestBody ColumnSimpForm columnSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            ColumnPersonalDataDto columnPersonalDataDto = columnService.getPersonalDataDto(columnSimpForm.getColumnId());
            return ResultWrapper.successWithData(columnPersonalDataDto);
        } catch (Exception e) {
            log.info("ColumnController.getDetailData");
            return ResultWrapper.failure(e.getMessage());
        }

    }

    /**
     * 《个人中心页面》
     * 根据前端传递的token获取个人信息页面展示的column
     *
     * @param token
     * @return
     */
    @ApiOperation("获取个人中心的栏目信息")
    @RequestMapping(path = "/personal/showColumns", method = RequestMethod.GET)
    public ResultWrapper getPersonalShowColumns(@RequestHeader("ANSWER_ACCESS_TOKEN") String token) {
        List<ColumnPersonalShowDto> columnPersonalShowDtoList = columnService.getPersonalShow(token);
        return ResultWrapper.successWithData(columnPersonalShowDtoList);
    }

    //获取二级菜单
    @ApiOperation("获取栏目回显的二级菜单")
    @RequestMapping(path = "/personal/getPlatesTwiceMenu", method = RequestMethod.GET)
    public ResultWrapper getPlatesTwiceMenu() {
        List<PlateDto> list = columnService.getPlatesTwiceMenu();
        return ResultWrapper.successWithData(list);
    }
}